﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ContaTelefone.Data
{
    public class CONTARepository
    {
        ContaTelefoneEntities entity;
        
        public CONTARepository()
        {
            entity = ContaTelefoneContext.getInstance();
        }

        public IQueryable<CONTA> GetCONTAs()
        {
            return ContaTelefoneContext.getInstance().CONTA;
        }

        public IList<CONTA> GetAll()
        {
            return ContaTelefoneContext.getInstance().CONTA.ToList();
        }

        public CONTA getCONTA(int pID_CONTA)
        {
            return entity.CONTA.SingleOrDefault(a => a.ID_CONTA == pID_CONTA);
        }

        public CONTA getCONTA(DateTime dtIni)
        {
            return entity.CONTA.SingleOrDefault(a => a.DATA_INICIO.Month == dtIni.Month && a.DATA_INICIO.Year == dtIni.Year);
        }

        public void Update()
        {
            try
            {
                entity.SaveChanges();
            }
            catch (Exception ex)
            {
                ContaTelefoneContext.removeInstance();
                throw ex;
            }
        }

        public void Add(CONTA pCONTA)
        {
            try
            {
                entity.AddToCONTA(pCONTA);
                entity.SaveChanges();
            }
            catch (Exception ex)
            {
                ContaTelefoneContext.removeInstance();
                throw ex;
            }
        }

        public void Delete(CONTA pCONTA)
        {
            entity.CONTA.DeleteObject(pCONTA);
            entity.SaveChanges();
        }
    }
}
